package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.etc.dao.BaseDao;
import com.etc.dao.MessageDao;
import com.etc.entity.Message;
import com.etc.entity.PageCount;

public class MessageDaoImpl extends BaseDao implements MessageDao{

	@Override
	public int insertMessage(Message message) {
		String sql="insert into messages_info(username,title,type,content,date,status) values(?,?,?,?,CURRENT_TIMESTAMP(),'正常')";
		Object[] args= {message.getUsername(),message.getTitle(),message.getType(),message.getContent()};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int deleteMessage(String title) {
		// TODO Auto-generated method stub
		String sql="delete from messages_info where title=?";
		Object[] args= {title};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int deleteAnsMessage(String title) {
		// TODO Auto-generated method stub
		String sql="delete from ansmessages_info where title=?";
		Object[] args= {title};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int deleteAnsAnsMessage(String title) {
		// TODO Auto-generated method stub
		String sql="delete from ansanswers_info where title=?";
		Object[] args= {title};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int closeMessage(int id) {
		// TODO Auto-generated method stub
		String sql ="update messages_info set status=? where id=?";
		Object[] args= {"封禁",id};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public int openMessage(int id) {
		// TODO Auto-generated method stub
		String sql ="update messages_info set status=? where id=?";
		Object[] args= {"正常",id};
		return this.myExecuteUpdate(sql, args);
	}

	@Override
	public Message findMessage(int id) {
		// TODO Auto-generated method stub
		String sql="select * from messages_info where id=? order by date desc";
		Object[] args= {id};
		return this.loadResult(Message.class, sql, args);
	}

	@Override
	public List<Message> findjinMessage() {
		// TODO Auto-generated method stub
		String sql="select * from messages_info where status='正常' order by date desc";
		
		return this.listResult(Message.class, sql, null);
	}

	@Override
	public List<Message> findUserMessage(String username) {
		// TODO Auto-generated method stub
		String sql="select * from messages_info where username=? ORDER BY date desc";
		Object[] args= {username};
		return this.listResult(Message.class, sql, args);
	}

	@Override
	public List<Message> searchAllMessage(int pageindex,int pagesize,String type) {
		// TODO Auto-generated method stub
		if(type==null) {
	String sql="select * from messages_info where status='正常' LIMIT ?,?";
		Object[] args= {(pageindex-1)*pagesize,pagesize};
		return this.listResult(Message.class, sql, args);
		}
		else {
			String sql="select * from messages_info where status='正常' and type=? limit ?,?";
			Object[] args= {type,(pageindex-1)*pagesize,pagesize};
			return this.listResult(Message.class, sql, args);
		}
	}

	@Override
	public List<Message> searchKeyMessage(String search) {
		// TODO Auto-generated method stub
		String sql="select * from messages_info where title like CONCAT('%',?,'%') ORDER BY date desc";
		Object[] args= {search};
		return this.listResult(Message.class, sql, args);
	}

	@Override
	public int getPageCount(int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		String sql="select count(*) from messages_info where status='正常'";
		
		return this.myExecuteUpdate(sql, null);
	}

	@Override
	public List<Message> findAllMessage(int pageNo, int pageSize, String type) throws SQLException {
		// TODO Auto-generated method stub
		// String sql="select * from messages_info where status='正常' limit ?,?";
		//Object[] args = {(pageNo-1)*pageSize,pageSize};
		if(type!=null && type!="") {
			String sql="select * from messages_info where status='正常' limit ?,?";
            Object[] args= {(pageNo-1)*pageSize,pageSize};
			return this.listResult(Message.class, sql, args);
		}
		else {
			String sql="select * from messages_info where status='正常' and type=? limit ?,?";
			Object[] args = {type,(pageNo-1)*pageSize,pageSize};
			return this.listResult(Message.class, sql, args);
		}
	}


	@Override
	public List<Message> findtypeMessage(String type) {
        String sql="select * from messages_info where type=? ORDER BY date desc";
        Object[] args= {type};
		return this.listResult(Message.class, sql, args);
	}

	@Override
	public Long countAll() {
		// TODO Auto-generated method stub
		String sql="select count(1) as totalCount from messages_info";
		Object[] args= {};
		return this.loadResult(PageCount.class, sql, args).getTotalCount();
	}

	@Override
	public List<Message> myMessage(String username,int pageIndex,int pageSize) {
		// TODO Auto-generated method stub
		if(username!=null) {
		String sql="select * from messages_info where username=? limit ?,?";
		Object[] args= {username,(pageIndex-1)*pageSize,pageSize};
		return this.listResult(Message.class, sql, args);
		}
		else {
			String sql="select * from messages_info limit ?,?";
			Object[] args= {(pageIndex-1)*pageSize,pageSize};
			return this.listResult(Message.class, sql, args);
		}
	}

	@Override
	public Long countUsername(String username) {
		// TODO Auto-generated method stub
		String sql="select count(1) as totalCount from messages_info where username=?";
		Object[] args= {username};
		return this.loadResult(PageCount.class, sql, args).getTotalCount();
	
	}

	@Override
	public List<Message> mySearch(String username, String search,int pageIndex,int pageSize) {
		// TODO Auto-generated method stub
		if(username!=null && search==null) {
		String sql="select * from messages_info where username=? limit ?,?";
		Object[] args= {username,(pageIndex-1)*pageSize,pageSize};
		return this.listResult(Message.class, sql, args);
		}
		/*
		 * else if(username==null && search!=null){ String sql =
		 * "select * from messages_info where content like CONCAT('%',?,'%') limit ?,?";
		 * Object[] args= {search,(pageIndex-1)*pageSize,pageSize}; return
		 * this.listResult(Message.class, sql, args); }
		 */
		else if(username==null&&search==null) {
			String sql = "select * from messages_info limit ?,?";
			Object[] args= {(pageIndex-1)*pageSize,pageSize};
			return this.listResult(Message.class, sql, args);
		}
		else {
			String sql = "select * from messages_info where username=? and content like CONCAT('%',?,'%') limit ?,?";
			Object[] args= {username,search,(pageIndex-1)*pageSize,pageSize};
			return this.listResult(Message.class, sql, args);
		}
	}

}
